﻿using ConfExpert.Forms.Base;

using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevTeam;

using System;
using System.Drawing;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo.Cikis
{
    public partial class KumasKabul : XForm
    {
        BindingSource bsIrsaliye, bsIrsaliyeLines, bsIrsaliyeLines2;
        public KumasKabul()
        {
            InitializeComponent();
        }

        private void KumasKabul_Load(object sender, EventArgs e)
        {
            bsIrsaliye = gvIrsaliye.Init("GetKumasIrsaliyeForTransfer", 0, null, false);
            bsIrsaliyeLines = gvIrsaliye.AddDetailGrid(gvIrsaliyeLines, "KumasIrsaliyeId");
            bsIrsaliyeLines2 = gvIrsaliyeLines.AddDetailGrid(gvIrsaliyeLines2, "KumasIrsaliyeId,ProductId");

            //gvIrsaliyeLines.SutunEkle("TopAdedi", typeof(decimal), "SUM(Child({FK}).Adet)");
            gvIrsaliye.AddColumn("Artikel", typeof(decimal), "COUNT(Child({FK}).ProductId)");
            gvIrsaliye.AddColumn("TopAdedi", typeof(decimal), "SUM(Child({FK}).Adet)");

            gvIrsaliye.MakeColumnInvisible("KumasIrsaliyeId", "isDelivered");
            gvIrsaliyeLines.MakeColumnInvisible("Hit");
            gvIrsaliyeLines2.MakeColumnInvisible("Hit");

            gleProduct.Edit.Assign(RI(Rp.V_Kumas));

            gvIrsaliye.Columns["IrsaliyeTarihi"].DisplayFormat.FormatType = DevExpress.Utils.FormatType.DateTime;
            gvIrsaliye.Columns["IrsaliyeTarihi"].DisplayFormat.FormatString = "d"; //G

            ((RepositoryItemComboBox)cmbTip.Edit).TextEditStyle = TextEditStyles.DisableTextEditor;

            txtRoll.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;

            btnGuncelle.ItemClick += btnGuncelle_ItemClick;
            cmbTip.EditValueChanged += cmbTip_EditValueChanged;
            gvIrsaliye.RowStyle += new DevExpress.XtraGrid.Views.Grid.RowStyleEventHandler(gvIrsaliye_RowStyle);
            splitContainerControl1.AutoSize();
            splitContainerControl2.AutoSize();

            if (ytk(149))
            {
                GridManager gm = GridManager.GetManager(gvIrsaliye);
                gm.GridMenu.AddMenu("İrsaliyeyi Onayla", IrsaliyeOnay);
            }


            gvIrsaliye.FormatAsNumber(0, "TopAdedi", "Artikel");
            gvIrsaliyeLines.FormatAsNumber(0, "Adet");
            gvIrsaliyeLines2.FormatAsNumber(1, "Qty");
            gvIrsaliye.AddSummary("TopAdedi", "Artikel");
            gvIrsaliyeLines.AddSummary("Adet");
            gvIrsaliyeLines2.AddSummary("Qty");

            gvIrsaliye.BestFitColumns();
            gvIrsaliyeLines.BestFitColumns();
            gvIrsaliyeLines2.BestFitColumns();
        }

        void gvIrsaliye_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            bool isDelivered = boolParse(gvIrsaliye.GetRowCellValue(e.RowHandle, "isDelivered"));
            if (isDelivered)
                e.Appearance.ForeColor = Color.Gray;
        }

        void IrsaliyeOnay(object sender, EventArgs e)
        {
            int IrsaliyeId = intParse(gvIrsaliye.GetRowCellValue(gvIrsaliye.FocusedRowHandle, "KumasIrsaliyeId"));
            ExecuteNonQuery("UpdIrsaliyeIsDelivered", IrsaliyeId);
            VeriyiGuncelle();
        }

        private string GetIrsaliyeId(string idxs)
        {
            foreach (int rowHandle in gvIrsaliye.GetSelectedRows())
                if (rowHandle > -1)
                    idxs += gvIrsaliye.GetRowCellValue(rowHandle, "KumasIrsaliyeId").ToString() + ",";

            if (idxs.Substring(idxs.Length - 1, 1) == ",")
                idxs = idxs.Substring(0, idxs.Length - 1);
            return idxs;
        }

        void cmbTip_EditValueChanged(object sender, EventArgs e)
        {
            gleProduct.EditValue =
            txtRoll.EditValue = null;

            switch (cmbTip.EditValue.ToStr())
            {
                case "Kumaş":
                    gleProduct.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    txtRoll.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    gvIrsaliyeLines.ChangeExpression("Hit", string.Empty);
                    break;

                case "Top":
                    gleProduct.Visibility = DevExpress.XtraBars.BarItemVisibility.Never;
                    txtRoll.Visibility = DevExpress.XtraBars.BarItemVisibility.Always;
                    gvIrsaliyeLines.ChangeExpression("Hit", "MAX(Child({FK}).Hit)");
                    break;
            }

        }

        void btnGuncelle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            VeriyiGuncelle();
        }
        private void VeriyiGuncelle()
        {
            int selIndex = -1;
            int Id = 0;
            switch (cmbTip.EditValue.ToStr())
            {
                case "Kumaş":
                    selIndex = 0;
                    Id = gleProduct.EditValue.ToInt();
                    break;

                case "Top":
                    selIndex = 1;
                    Id = txtRoll.EditValue.ToInt();
                    break;
            }

            btnGuncelle.Enabled = false;
            gvIrsaliye.RefreshDataSource("GetKumasIrsaliyeForTransfer", selIndex, Id, chkKapanlariGoster.Checked);
            btnGuncelle.Enabled = true;

            gvIrsaliyeLines.FocusedRowHandle = gvIrsaliyeLines.LocateByDisplayText(0, gvIrsaliyeLines.Columns["Hit"], "1");
            gvIrsaliyeLines2.FocusedRowHandle = gvIrsaliyeLines2.LocateByDisplayText(0, gvIrsaliyeLines2.Columns["Hit"], "1");
            gvIrsaliyeLines.BestFitColumns();
        }

        private void chkKapanlariGoster_CheckedChanged(object sender, EventArgs e)
        {
            VeriyiGuncelle();
        }
    }
}